package com.own.component.token.config;

import com.own.component.autoconfig.property.token.TokenProperty;
import com.own.component.store.core.method.StoreSet;
import com.own.component.store.core.method.StoreValue;
import com.own.component.token.base.AbstractStoreTokenManager;
import com.own.component.token.base.RequestTokenManager;
import com.own.component.token.base.StoreTokenManager;
import com.own.component.token.base.TokenBuildPolicy;
import com.own.component.token.impl.DefaultHeaderRequestTokenManager;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * DefaultTokenConfig
 *
 * @author chenxueli
 * @date 2023-11-07 19:49:04
 */
@SpringBootConfiguration
@DependsOn("tokenInstance")
public class DefaultTokenConfig {

    @Resource
    public TokenProperty tokenProperty;

    @Resource
    public TokenBuildPolicy tokenBuildPolicy;

    @Resource
    public StoreSet<String> tokenStore;

    @Resource
    public StoreValue<String> userIdStore;

    @Bean
    @ConditionalOnMissingBean(value = StoreTokenManager.class)
    public StoreTokenManager storeTokenManager() {
        return new AbstractStoreTokenManager(tokenStore, userIdStore, tokenBuildPolicy) {
        };
    }

    @Bean
    @ConditionalOnMissingBean(value = RequestTokenManager.class)
    public RequestTokenManager requestTokenManager() {
        return new DefaultHeaderRequestTokenManager(tokenProperty.getTokenRequestField());
    }

    @Bean
    @ConditionalOnMissingBean(value = TokenBuildPolicy.class)
    public TokenBuildPolicy tokenBuildPolicy() {
        return (userId, client) -> UUID.randomUUID().toString();
    }

}
